{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{# @var string                                         icon #}
{# @var \Glpi\Form\Desination\FormDestinationInterface default_destination_object #}
{# @var \Glpi\Form\Form                                form #}
{# @var \Glpi\Form\Desination\FormDestination          destinations #}
{# @var int|null                                       active_destination #}
{# @var boolean                                        can_update #}

<div class="py-2 px-3">
    <h2 class="d-flex">
        <i class="{{ icon }} me-2"></i>
        {{ _n("Item to create", "Items to create", get_plural_number()) }}
    </h2>

    {% for warning in warnings %}
        <div class="alert alert-warning d-flex align-items-center mb-3" role="alert">
            <i class="ti ti-alert-triangle me-2"></i>
            {{ warning }}
        </div>
    {% endfor %}

    <div class="d-flex">
        {# Add Form #}
        {% for type, label in available_destinations_types %}
            <form
                id="add_destination_{{ type }}"
                method="POST"
                action="{{ path('/Form/%d/Destination/Add'|format(form.getID())) }}"
            >
                <input type="hidden" name="name" value="{{ label }}"/>
                <input type="hidden" name="_glpi_csrf_token" value="{{ csrf_token() }}"/>
                <input type="hidden" name="add" value=""/>
            </form>
        {% endfor %}

        <div class="btn-group me-2">
            {% for type, label in available_destinations_types %}
                <button
                    form="add_destination_{{ type }}"
                    class="btn"
                    name="itemtype"
                    type="submit"
                    value="{{ type }}"
                >
                    <i class="ti ti-plus me-1"></i> {{ __("Add %s"|format(label)) }}
                </button>
            {% endfor %}
        </div>
    </div>

    {% if destinations|length == 0 %}
        <p class="empty-subtitle text-muted mt-3 ms-1">
            {{ __("No items will be created for this form.") }}
        </p>
    {% else %}
        <section class="accordion mt-4" id="glpi-destinations-accordion" aria-label="{{ __("Form destinations") }}">
            {% for destination in destinations %}
                {% set is_expanded = active_destination == destination.getID() or destinations|length == 1 %}
                {% set concrete_destination = destination.getConcreteDestinationItem() %}
                {% set rand = random() %}
                <section
                    class="accordion-item"
                    aria-label="{{ destination.getName() }}"
                >
                    <div class="accordion-header">
                        <div
                            class="accordion-button {{ is_expanded ? '' : 'collapsed' }}"
                            role="button"
                            tabindex="0"
                            data-bs-toggle="collapse"
                            data-bs-target="#glpi-destinations-accordion-collapse-{{ rand }}-{{ loop.index }}"
                            aria-expanded="{{ is_expanded ? 'true' : 'false' }}"
                            data-glpi-destination-click-on-space {# Simulate how space would behave in a button instead of a div #}
                        >
                            <i class="{{ concrete_destination.getIcon() }}"></i>
                            <span
                                class="m-0 lh-base fw-normal glpi-form-destination-name"
                            >
                                {{ destination.getName() }}
                            </span>
                            <div data-glpi-editor-destination-badges-container>
                                {% set selected_strategy = destination.getConfiguredCreationStrategy() %}
                                {% for strategy in enum_cases('Glpi\\Form\\Condition\\CreationStrategy') %}
                                    {% set is_visible = selected_strategy == strategy %}
                                    {% set display_class = is_visible ? 'd-flex' : 'd-none' %}
                                    <span
                                        class="{{ display_class }} badge bg-secondary-lt d-flex align-items-center ms-2"
                                        data-glpi-editor-condition-badge="{{ strategy.value }}"
                                    >
                                        <i class="{{ strategy.getIcon() }} me-1"></i>
                                        <span>{{ strategy.getLabel() }}</span>
                                    </span>
                                {% endfor %}
                            </div>
                            <input
                                aria-label="{{ __("Form destination name") }}"
                                type="text"
                                name="name"
                                form="form-destination-{{ destination.getID() }}"
                                value="{{ destination.getName() }}"
                                class="form-control me-4 fw-normal glpi-form-destination-name"
                                style="font-weight: inherit;"
                                {# Fake collaspe needed to avoid triggering the parent accordion when clicking inside #}
                                data-bs-toggle="collapse"
                                data-bs-target=""
                            />
                        </div>
                    </div>
                    <div
                        id="glpi-destinations-accordion-collapse-{{ rand }}-{{ loop.index }}"
                        class="accordion-collapse collapse {{ is_expanded ? 'show' : '' }}"
                        data-bs-parent="#glpi-destinations-accordion"
                    >
                        <div class="accordion-body p-0">
                            <form id="form-destination-{{ destination.getID() }}">
                                <div class="overflow-x-hidden px-4">
                                    {{ concrete_destination.renderConfigForm(
                                        form,
                                        destination,
                                        destination.getConfig(),
                                    )|raw }}
                                    {% if concrete_destination.useDefaultConfigLayout() and can_update %}
                                        <div class="mt-3 mb-3">
                                            {{ include('pages/admin/form/form_destination_actions.html.twig', {
                                                form: form,
                                                destination: destination
                                            }, with_context = false) }}
                                        </div>
                                    {% endif %}
                                </div>

                                {# Hidden values #}
                                <input type="hidden" name="id" value="{{ destination.getID() }}"/>
                                <input type="hidden" name="_glpi_csrf_token" value="{{ csrf_token() }}"/>
                            </form>
                        </div>
                    </div>
                </section>
            {% endfor %}
        </section>
    {% endif %}
</div>

<script>
    import("/js/modules/Forms/DestinationAutoConfigController.js").then((m) => {
        new m.GlpiFormDestinationAutoConfigController();
    });

    import("/js/modules/Forms/DestinationConditionController.js").then((m) => {
        new m.GlpiFormDestinationConditionController();
    });
</script>
